#pragma once

#include <qbprotocol/include/SSFilters.h>
#include <qbprotocol/include/SSQBModel.h>

#include <algorithm>
#include <list>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include "bondlib.h"

class S_BONDLIB_EXPORT CThirdPartyGuarantee {
  typedef std::unordered_map<std::string, QBThirdPartyGuarantee>
      ThirdPartyGuaranteeMap;
  typedef std::vector<const char*> InstitutionCodeList;

 public:
  static CThirdPartyGuarantee& instance();
  void ClearData();
  void UpdateData(xQBThirdPartyGuaranteeListAck_c* pAck);
  bool CheckBond(int bondIndex, const BLHCONDITION& cond);

 private:
  CThirdPartyGuarantee();
  virtual ~CThirdPartyGuarantee();

  std::string GetCombBondKey(int bondIndex) const;
  bool IsNationalGuarantee(int type, const QBThirdPartyGuarantee& data);
  bool IsNationalGuaranteeType(int type) const;
  const InstitutionCodeList& ToInstitutionCode(int type) const;
  bool IsRegionalGuarantee(int type, const QBThirdPartyGuarantee& tpg);
  bool IsRegionalGuaranteeType(int type) const;
  bool IsOtherArea(int type, const char* area) const;
  const char* ToArea(int type) const;

 private:
  std::mutex m_mutex;
  ThirdPartyGuaranteeMap m_mapData;
  std::map<int, InstitutionCodeList> m_nationalMap;
  std::set<std::string> m_setArea;
};
